var models = require('../db');
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('../sqlMap');
var url = require('url');

// 连接数据库
var conn = mysql.createConnection(models.mysql);
conn.connect();
var jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({ code: '1', msg: '操作失败' })
    } else {
        res.json(ret)
    }
}
// 查询teachers列表接口
router.get('/teachers', (req, res) => {
    var sql = $sql.teachers

    conn.query(sql, req.query.approve_subject, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
});

//查询老师详细信息
router.get('/teacherDetail', (req, res) => {
    var sql = $sql.teacherDetail
    const id = req.query.id
    conn.query(sql, id, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
});

//查询主科目分类
router.get('/categories', (req, res) => {
    var sql = $sql.categories
    conn.query(sql, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
});

//查询小学科目
router.get('/categories_primarySchool', (req, res) => {
    var sql = $sql.categories_primarySchool
    conn.query(sql, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
});
//查询初中科目
router.get('/categories_middleSchool', (req, res) => {
    var sql = $sql.categories_middleSchool
    conn.query(sql, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
});
//查询高中科目
router.get('/categories_highSchool', (req, res) => {
    var sql = $sql.categories_highSchool
    conn.query(sql, function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
});

//查询对应科目老师
router.get('/approve_subject_teachers', (req, res) => {
    var sql = $sql.approve_subject_teachers
    switch (req.query.approve_subject) {
        case 'yw':
            req.query.approve_subject = '语文'
            break;
        case 'yy':
            req.query.approve_subject = '英语'
            break;
        case 'sx':
            req.query.approve_subject = '数学'
            break;
        case 'wl':
            req.query.approve_subject = '物理'
            break;
        case 'sw':
            req.query.approve_subject = '生物'
            break;
        case 'hx':
            req.query.approve_subject = '化学'
            break;

        default:
            break;
    }
    conn.query(sql, '%' + req.query.approve_subject + '%', function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res, result)
        }
    })
});
module.exports = router